#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE 1024

int main() {
	FILE *fd;
	FILE *fp;
	FILE *out;
	char filename[LINE];
	char buf[LINE];
	int sample[2000];
	int len = 0;
	int rssi = 0;
	int lineNum = 0;
	unsigned char r =0;
	char firstLine = 1;

	fd = fopen("fileList.txt","r");
	if(fd == NULL) {
		printf("cannot open fileList.txt\n");
		exit(1);
	}
	while(fgets(filename,LINE,fd)!=NULL){
		len = strlen(filename);
		filename[len-1] = '\0';
		if(filename[len-2]!='g'){
			continue;
		}
		printf("%s\n",filename);

		fp = fopen(filename, "r");
		if(fp == NULL) {
			printf("cannot open %s\n",filename);
			exit(1);
		}

		filename[len-1]='n';
		filename[len]='u';
		filename[len+1]= 'm';
		filename[len+2]= '\0';
		out = fopen(filename,"w");
		while(fgets(buf,LINE,fp)!=NULL){
			if(firstLine!=0) {
				firstLine = 0;
				continue;
			}
			len = strlen(buf);
			buf[len-1] = '\0';
			r = (unsigned int) (atoi(buf));	
			r -= 127;
			if(r>=127) {
				r = r-256-45;
			}
			else {
				r = r-45;
			}
			rssi = r-255;
			fprintf(out,"%i \t %u \t  %i\n",lineNum++, r, rssi);
		}
		fclose(fp);		
		firstLine = 1;
		lineNum = 0;
	}
	fclose(fd);
}
